package promotion

import (
	"bp/internal/render"
	"bp/internal/service/param"
	"bp/internal/util"
	promotionGrpc "bp/rpc/gen/webapi/promotion"
	"context"
)

// 获取促销活动
func (s *PromotionWebApiImp) GetPromotion(ctx context.Context, req *promotionGrpc.GetPromotionReq) (res *promotionGrpc.GetPromotionRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("promotion_imp/query.GetPromotion=>%+v", req)
	defer render.FriendlyEntityError(&err, EntityName)
	res = &promotionGrpc.GetPromotionRes{}
	q := s.dao.Q

	promotionID, err := s.VldGetPromotionReq(ctx, q, req)
	if err != nil {
		return
	}
	promotionPtr, err := s.entityHub.PromotionService.GetPromotionPtr(ctx, q, promotionID)
	if err != nil {
		return
	}
	itemCategoryConfPtrs, err := s.entityHub.PromotionService.ListItemCategoryConfigurationPtrsByPromotionID(ctx, q, promotionID)
	if err != nil {
		return
	}
	itemConfPtrs, err := s.entityHub.PromotionService.ListItemConfigurationPtrsByPromotionID(ctx, q, promotionID)
	if err != nil {
		return
	}
	res.Promotion = s.ConvPromotionPtrEntityToPb(itemCategoryConfPtrs, itemConfPtrs)(promotionPtr)
	return
}

// ListPromotions 列出促销活动
func (s *PromotionWebApiImp) ListPromotions(ctx context.Context, req *promotionGrpc.ListPromotionsReq) (res *promotionGrpc.ListPromotionsRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("promotion_imp/query.ListPromotions=>%+v", req)
	res = &promotionGrpc.ListPromotionsRes{}
	q := s.dao.Q

	cPtr := param.GenListCommonParamFromContext(ctx)
	ptr, err := s.VldListPromotionsReq(ctx, q, req)
	if err != nil {
		return
	}
	total, promotionPtrs, err := s.entityHub.PromotionService.ListPromotionPtrs(ctx, q, cPtr, ptr)
	if err != nil {
		return
	}

	var itemCategoryMap param.EntityIDToItemCategoryConfigurationPtrsMap
	var itemMap param.EntityIDToItemConfigurationPtrsMap
	if req.WithItemCategoryConfs {
		itemCategoryMap, err = s.entityHub.PromotionService.GetMapOfPromotionIDToItemCategoryConfigurationPtrsByPromotionPtrs(ctx, q, promotionPtrs)
		if err != nil {
			return
		}
	}
	if req.WithItemConfs {
		itemMap, err = s.entityHub.PromotionService.GetMapOfPromotionIDToItemConfigurationPtrsByPromotionPtrs(ctx, q, promotionPtrs)
		if err != nil {
			return
		}
	}

	res.Total = int32(total)
	res.Rows = s.ConvPromotionPtrArrayEntityToPb(itemCategoryMap, itemMap)(promotionPtrs)
	return
}

// ListPromotionWithStats 列出促销活动及其统计
func (s *PromotionWebApiImp) ListPromotionWithStats(ctx context.Context, req *promotionGrpc.ListPromotionWithStatsReq) (res *promotionGrpc.ListPromotionWithStatsRes, err error) {
	slog := util.NewContextLogger(ctx, s.slog)
	slog.Infof("promotion_imp/query.ListPromotionWithStats=>%+v", req)
	res = &promotionGrpc.ListPromotionWithStatsRes{}
	q := s.dao.Q

	cPtr := param.GenListCommonParamFromContext(ctx)
	ptr, err := s.VldListPromotionWithStatsReq(ctx, q, req)
	if err != nil {
		return
	}
	total, promotionWithStatPtrs, err := s.entityHub.PromotionService.ListPromotionWithStatPtrs(ctx, q, cPtr, ptr)
	if err != nil {
		return
	}
	var itemCategoryMap param.EntityIDToItemCategoryConfigurationPtrsMap
	var itemMap param.EntityIDToItemConfigurationPtrsMap
	if req.WithItemCategoryConfs {
		itemCategoryMap, err = s.entityHub.PromotionService.GetMapOfPromotionIDToItemCategoryConfigurationPtrsByListPromotionWithStatRowPtrs(ctx, q, promotionWithStatPtrs)
		if err != nil {
			return
		}
	}
	if req.WithItemConfs {
		itemMap, err = s.entityHub.PromotionService.GetMapOfPromotionIDToItemConfigurationPtrsByListPromotionWithStatRowPtrs(ctx, q, promotionWithStatPtrs)
		if err != nil {
			return
		}
	}

	res.Total = int32(total)
	res.Rows = s.ConvPromotionWithStatPtrArrayEntityToPb(itemCategoryMap, itemMap)(promotionWithStatPtrs)
	return
}
